import { defineStore } from 'pinia';

import type { StorageLike } from 'pinia-plugin-persistedstate';
import SecureLS from 'secure-ls';
// encryptionSecret:自定义密钥
const ls = new SecureLS({
  isCompression: false,
  encryptionSecret: 'yiweiyihang',
});
const st: StorageLike = {
  setItem(key: string, value: string) {
    ls.set(key, value);
  },
  getItem(key: string): string | null {
    return ls.get(key);
  },
};
interface State {
  locale: string;
  theme: string;
}
const useCommonStore = defineStore('common', {
  state: () => {
    return {
      locale: 'zh_CN', // en
      theme: 'light',
      customColor: '',
    };
  },
  getters: {},
  actions: {
    changeLocal(value: string) {
      this.locale = value;
    },
    changeTheme(value: string) {
      // console.log('change', value);
      this.theme = value;
    },
    changeCustomColor(value: string) {
      this.customColor = value;
    },
  },
  // 所有数据持久化
  persist: {
    // storage: st, // 上面申明的类型
    // storage: localStorage,
    // key: 'counter',
  },
  // 持久化存储插件其他配置
  // persist: {
  // 修改存储中使用的键名称，默认为当前 Store的 id
  // key: 'storekey',
  // 修改为 sessionStorage，默认为 localStorage
  //   storage: window.sessionStorage,
  // 部分持久化状态的点符号路径数组，[]意味着没有状态被持久化(默认为undefined，持久化整个状态)
  //   paths: ['nested.data'],
  // },
});

export default useCommonStore;
